package com.caimiao.miao.page.ticket;

import android.content.Context;
import android.content.Intent;

import com.caimiao.miao.bean.Ticket;
import com.caimiao.miao.bean.TicketOut;
import com.caimiao.miao.interfaces.BaseActivity;
import com.caimiao.miao.interfaces.BasePresenterImpl;
import com.caimiao.miao.library.bluetooth.state.IBtState;
import com.caimiao.miao.http.rxjava.transformer.SchedulerTransformer;
import com.caimiao.miao.library.utils.tip.LogUtil;
import com.caimiao.miao.service.MiaoService;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class TicketPresenter extends BasePresenterImpl<ITicketContact.View, ITicketContact.Model> implements ITicketContact.Presenter {
    public TicketPresenter(ITicketContact.View mView) {
        super(mView);
    }

    @Override
    protected ITicketContact.Model getModel() {
        return new TicketModel();
    }

    @Override
    public void start(Context context) {
        mActivity = new WeakReference<>(context);
        TicketOut ticket = ((BaseActivity) context).getIntent().getParcelableExtra(TicketActivity.EXTRA_DATA);
        if (ticket == null) {
            mView.close(null);
            return;
        }
        init(ticket);
    }


    @Override
    public void setBinder(MiaoService.LocalBinder binder) {
        super.setBinder(binder);
        startTicketOut();
    }


    @Override
    public void setCloseTicket(Context context, int state) {
        mModel.updateBeforeClose(mActivity.get(), state);
        mBinder.getService().postTickOut();

        TicketOut ticket = mModel.getNextTicket();
        if (ticket == null) {
            mView.close(null);
        } else {
            init(ticket);
            startTicketOut();
        }
    }

    @Override
    public void restartTakeTicket() {
        mBinder.getService().restartTakeTicket();
    }

    @Override
    public void setContinue(int state) {
        if (state != 2)
            mBinder.getService().setReOutTicket();
    }

    @Override
    public void onNewIntent(Intent intent) {
        TicketOut ticket = intent.getParcelableExtra(TicketActivity.EXTRA_DATA);
        if (ticket != null) mModel.addTicket(ticket);
    }

    @Override
    public void detach() {
        super.detach();
        mBinder = null;
    }

    private void init(TicketOut ticket) {
        mModel.setTicket(ticket);
        mView.init(ticket);
        setTicketNum(mModel.getMaxNum());
    }


    private void setTicketNum(int num) {
        mView.setTicketNum(String.valueOf(num) + " / " + String.valueOf(mModel.getMaxNum()));
    }

    //开始出票
    private void startTicketOut() {
        Disposable disposable = mBinder.getService()
                .setOutTicket(mModel.getMaxNum())
                .compose(SchedulerTransformer.applyIoAndUi())
                .subscribe(new Consumer<IBtState>() {
                    @Override
                    public void accept(IBtState iBtState) throws Exception {
                        LogUtil.e("Ticket", "STATE: " + iBtState.getState());
                        if (iBtState.getCode() == IBtState.CODE_TICKET) {
                            if (iBtState.getState() == 0) {//出票成功
                                try {
                                    int remainCount = Integer.valueOf(iBtState.getMsg());
                                    mModel.setRemainCount(remainCount);
                                    if (remainCount == 0) {
                                        setCloseTicket(mActivity.get(), iBtState.getState());//出票完成
                                    } else {
                                        setTicketNum(remainCount);
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            mView.setTicketState(iBtState.getState());
                        }
                    }
                });
        addDisposable(disposable);
    }

}
